<?php
/**
 * Description: 微信移动支付通知url
 * User: devforma
 * Date: 18/11/17
 * Time: 10:34
 */


require PUBLIC_DIR . '/payment/includes/WeixinPay.class.php';
require PUBLIC_DIR . '/payment/includes/OrderProcess.class.php';

class WeixinMobileNotifyUrl implements iController {
    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        $payment = new WeixinPay();

        $rawXml = file_get_contents('php://input');
        if (empty($rawXml))
            return $payment->array2xml(array('return_code' => 'FAIL'));

        $data = $payment->xml2array($rawXml);
        if ($data && $payment->verifyReturn($data)) {
            $orderProcess = new OrderProcess($data['out_trade_no'], $data['transaction_id'], '', 'weixin');

            if ($orderProcess->isExists() && !$orderProcess->isPurchased()) {
                $orderProcess::clearCache($data['out_trade_no']);

                if ($orderProcess->finish()) {
                    return $payment->array2xml(array('return_code' => 'SUCCESS'));
                }
            }
        }

        return $payment->array2xml(array('return_code' => 'FAIL'));
    }
}
